package com.haiyou.dc.thread.test3;

/**
 * ThreadLocal：上下文，所处的环境，看谁调用属于谁
 * 特别属于构造函数，容易出错混淆
 */
public class TestThreadLocal03 {


    private static ThreadLocal<Integer> threadLocal = ThreadLocal.withInitial(() -> 1);

    public static void main(String[] args) {

        //main线程调用new MyRun()，构造器属于main线程
        //start方法调用run，所以属于myRun线程
        new Thread(new MyRun()).start();
        new Thread(new MyRun()).start();
    }

    static class MyRun implements Runnable {

        public MyRun(){
            threadLocal.set(-100);
            System.out.println(Thread.currentThread().getName() + "------>" + threadLocal.get());
        }

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + "------>" + threadLocal.get());

        }
    }

}
